package com.vueperson.vueperson.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Entity
@Component
@Table(name = "vueUser")

@Data
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uid")
    private Integer id;
     String name;
     String address;
     String favorites;
     String email;
     String username;
     String password;
     @CreatedDate
     Long createTime;

    /**
     * 修改时间
     */
    @LastModifiedDate
    @Column(name = "modify_time")
    Date modifyTime;

     Boolean enabled;
     Boolean locked;

    /**
     * 多个用户对应一个角色
     * 注解形式配置多对一
     *  1,配置表关系
     *  2,配置外键
     */
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "rid")
    Role role;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
       authorities.add(new SimpleGrantedAuthority(role.getName()));
        return authorities;
    }
    //当前账户是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //当前账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }
    //当前账户密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
